﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SMS.Common.Silverlight;
using SMS.Controls.AppHealthSVC;
using SMS.Entities;

namespace SMS.Model
{

    public class AppHealthModel
    {
        ServiceProxy<AppHealthServiceClient> proxy;
        public event Action<System.Collections.ObjectModel.ObservableCollection<LoginHistory>> OnGetLoginHistoryCompleted;
        public event Action<System.Collections.ObjectModel.ObservableCollection<ExceptionLog>> OnGetExceptionHistoryCompleted;
        public event Action<System.Collections.ObjectModel.ObservableCollection<UserActivity>> OnGetUserActivityCompleted;
        public event Action<SMSException> OnServiceError;
        public AppHealthModel()
        {
            proxy = new ServiceProxy<AppHealthServiceClient>("Service/AppHealthService.svc");
            proxy.Client.GetUserActivityCompleted += new EventHandler<GetUserActivityCompletedEventArgs>(Client_GetUserActivityCompleted);
            proxy.Client.GetLoginHistoryCompleted += new EventHandler<GetLoginHistoryCompletedEventArgs>(Client_GetLoginHistoryCompleted);
            proxy.Client.GetExceptionHistoryCompleted += new EventHandler<GetExceptionHistoryCompletedEventArgs>(Client_GetExceptionHistoryCompleted);
        }

        void Client_GetExceptionHistoryCompleted(object sender, GetExceptionHistoryCompletedEventArgs e)
        {
            if (e.serviceError == null)
            {
                OnGetExceptionHistoryCompleted(e.Result);
            }
            else if (OnServiceError != null)
                OnServiceError(e.serviceError);
        }

        void Client_GetLoginHistoryCompleted(object sender, GetLoginHistoryCompletedEventArgs e)
        {
            if (e.serviceError == null)
            {
                OnGetLoginHistoryCompleted(e.Result);
            }
            else if (OnServiceError != null)
                OnServiceError(e.serviceError);
        }

        void Client_GetUserActivityCompleted(object sender, GetUserActivityCompletedEventArgs e)
        {
            if (e.serviceError == null)
            {
                OnGetUserActivityCompleted(e.Result);
            }
            else if (OnServiceError != null)
                OnServiceError(e.serviceError);
        }

        public void GetUserActivity(DateTime fromDate, DateTime toDate)
        {
            proxy.Client.GetUserActivityAsync(App.LoggedinUserName, fromDate, toDate);
        }

        public void GetLoginHistory(DateTime fromDate, DateTime toDate)
        {
            proxy.Client.GetLoginHistoryAsync(App.LoggedinUserName, fromDate, toDate);
        }

        public void GetExceptionHistory(DateTime fromDate, DateTime toDate)
        {
            proxy.Client.GetExceptionHistoryAsync(App.LoggedinUserName, fromDate, toDate);
        }
    }
}
